

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

class Dialogs {
  static Future<bool> showQuestionDialog(BuildContext context, {String title, String content}) async {
    await showDialog(context: context, builder: (context) {
      return CupertinoAlertDialog(
        title: title == null ? SizedBox() : Text(title, style: Theme.of(context).textTheme.subtitle1,),
        content: Text(content, style: Theme.of(context).textTheme.subtitle2,),
        actions: <Widget>[
          CupertinoButton(child: Text("ياق", style: Theme.of(context).textTheme.subtitle1,), onPressed: () => Navigator.of(context).pop(false),),
          CupertinoButton(child: Text("ھەئە", style: Theme.of(context).textTheme.subtitle1,), onPressed: () => Navigator.of(context).pop(true)),
        ],
      );
    }, barrierDismissible: false);
  }

  static Future<T> runWithProgressDialog<T>({BuildContext context, Future<T> action(), bool waitUserClicksWhenErrorOccurred = true}) async {
    try {
      T data;
      return await showDialog(context: context, builder: (context) => FutureBuilder<T>(
        future: action(),
        builder: (context, snapshot) {
          Widget title = Text("Processing...");
          Widget content = Center(child: CircularProgressIndicator(),);
          var actions = <Widget>[];

          if (snapshot.hasError) {
            title = Text("OOPS... there has a problem");
            content = Text(snapshot.error.toString());
            if (waitUserClicksWhenErrorOccurred) {
              actions.add(FlatButton(
                child: Text("Okay then"),
                onPressed: () {
                  Navigator.of(context).pop(null);
                },
              ));
            }
          }
          if (snapshot.hasData) {
            data = snapshot.data;
            SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
              Navigator.of(context).pop(data);
            });
          }
          return CupertinoAlertDialog(
            title: title,
            content: content,
            actions: actions,
          );
        },
      ), barrierDismissible: false);
    }
    catch (error) {

    }
    return null;
  }
}